<?php
require_once 'config.php';
require_once 'signaTure.php';
require_once 'notify/postRequest.php';

class Nowpay
{
	
	public static function pay($mhtOrderName,$mhtOrderAmt,$mhtOrderDetail,$payType,$outputType,$mhtOrderNo,$front_notify_url){
		if(empty($payType)) {
			die('请选择支付方式');
		}
		//判断支付类型
		switch($payType) {
			case 'weixin':
				$appid = Config::$wxAppId;
				$key = Config::$wxKey;
				$payChannelType = 13;
				break;
			case 'zhifubao':
				$appid = Config::$zfbAppId;
				$key = Config::$zfbKey;
				$payChannelType = 12;
				break;
			case 'yinlian':
				$appid = Config::$ylAppId;
				$key = Config::$ylKey;
				$payChannelType = 20;
				break;			
		}
		//$outputType = '0';
		$consumerCreateIp = '';
		if($payType == 'weixin' && $outputType == '0') {
			$outputType = '0';
			$consumerCreateIp = self::getUserIp(); //需上传用户真实ip
		} else if($payType == 'weixin' && $outputType == '1') {
			$outputType = '2';
		} else if($payType == 'zhifubao' && $outputType == '0') {
			$outputType = '0';
		} else if($payType == 'zhifubao' && $outputType == '1') {
			$outputType = '1';
		}

		$req=array();
		$req["appId"]             = $appid;
		$req["deviceType"]        = Config::TRADE_DEVICE_TYPE;
		//$req["frontNotifyUrl"]    = Config::$front_notify_url;
		$req["frontNotifyUrl"]    = $front_notify_url;//商户前台通知URL
		$req["funcode"]           = Config::TRADE_FUNCODE;
		$req["mhtCharset"]        = Config::TRADE_CHARSET;
		$req["mhtCurrencyType"]   = Config::TRADE_CURRENCYTYPE;
		$req["mhtOrderAmt"]       = $mhtOrderAmt;
		$req["mhtOrderDetail"]    = $mhtOrderDetail;
		$req["mhtOrderName"]      = $mhtOrderName;
		$req["mhtOrderNo"]        = $mhtOrderNo;
		$req["mhtOrderStartTime"] = date("YmdHis");
		$req["mhtOrderTimeOut"]   = Config::$trade_time_out;
		$req["mhtOrderType"]      = Config::TRADE_TYPE;
		$req["mhtReserved"]       = "test";
		$req["mhtSignType"]       = Config::TRADE_SIGN_TYPE;
		$req["notifyUrl"]         = Config::$back_notify_url;//商户后台通知URL
		$req["outputType"]        = $outputType;//   0 默认值    // 2  微信deeplink模式
		$req["payChannelType"]    = $payChannelType; //12 支付宝  //13 微信 //20 银联  //25  手Q
		$req["version"]           = "1.0.0";
		$req["consumerCreateIp"]  = $consumerCreateIp; //微信必填// outputType=2时 无须上送该值

		$info = new SignaTure;
		$req_str = $info -> getToStr($req, $key);
		if($payType == 'weixin' && $outputType == '0') {
			header("location:".Config::TRADE_URL."?".$req_str);
			die();
		}
		$post = new PostRequest;
		$res = $post -> post(Config::TRADE_URL, $req_str);
		
		$code = (bool)stripos($res, '&tn=');
		if($code) {
			$arr = explode('&', $res);
			$gettn = '';
			foreach($arr as $v) {
				$tn = explode('=', $v);
				if($tn[0] == 'tn'){
					$gettn = $tn[1];
				} 
			}
			echo "请点击链接进行支付：<a href='". urldecode($gettn) ."'>点我支付</a>";
		} else {
			echo $res;
		}
	}


	public static function notify($str, $appkey){
		if($str != '') {
			$arr = explode('&', $str);
			$nowArr = array();
			foreach($arr as $v) {
				$kv = explode('=', $v);
				$nowArr[$kv[0]] = $kv[1];
			}
			ksort($nowArr);

			$newstr = '';
			foreach($nowArr as $key => $value) {
				if($value == '' || $key == 'signature') {
					continue;
				}
				$newstr .= $key.'='.urldecode($value).'&';
			}
			$newstr .= md5($appkey);

			if( $nowArr['signature'] == md5($newstr) ) {
				return true;
			} else {
				return false;
			}
		}
	}

	private static function getUserIp()
	{
		/**
		$info = file_get_contents('http://myip.ipip.net');
	 	$ipstr = explode('：',$info);
	 	$ip = explode(' ', $ipstr[1]);
	 	return $ip[0];
		**/
		if (getenv('HTTP_CLIENT_IP')) {
		$ip = getenv('HTTP_CLIENT_IP');
		}
		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
		$ip = getenv('HTTP_X_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_X_FORWARDED')) {
		$ip = getenv('HTTP_X_FORWARDED');
		}
		elseif (getenv('HTTP_FORWARDED_FOR')) {
		$ip = getenv('HTTP_FORWARDED_FOR');

		}
		elseif (getenv('HTTP_FORWARDED')) {
		$ip = getenv('HTTP_FORWARDED');
		}
		else {
		$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
}
?>